
import "./SignUpLoading.scss"

import { useMount } from 'ahooks'
import { Button, Alert, Modal } from "antd"
import { useRequest } from 'alova/client'
import { lazy, Suspense, useState } from 'react'
import { useNavigate } from 'react-router-dom'

import { Http } from '../../Utils/Api'
import { getSignUpBack } from '../../Utils'
import { modelStore, drawerStore, fromStore } from '../../Config/zustand'

const LoadSvg = lazy(() => import('../../View/Loading/LoadSvg'))

const SignUpLoading = ({ data, Off }) => {
    const { updateModel } = modelStore()
    const { updateDrawer } = drawerStore()
    const { updateFromData } = fromStore()

    const [ListByUser, setListByUser] = useState([])
    const [open, setOpen] = useState(false)

    const [isPassword, setIsPassword] = useState(true)

    const onNavigate = useNavigate()

    const [loading, setLoading] = useState(false)

    const { send } = useRequest(({ url, params }) => Http({ url, params }), { immediate: false })
    const onAdmin = () => onNavigate('/Admin')
    const onStart = () => onNavigate('/Start/Home')

    useMount(async () => {
        updateModel({})
        updateDrawer({})
        updateFromData({})

        send({ url: 'getListByUser', params: data }).then(res => {
            if (res.data.list.length === 1) signUp(res.data.list[0].uuid)
            else {
                setListByUser(res.data.list)
                setOpen(true)
            }
        })
    })

    const signUp = async (uuid) => {
        setOpen(false)
        try {
            const tonzoc = await send({ url: 'posToken', params: { ...data, OrganizationUuid: uuid } })
            const { token_type, access_token } = tonzoc.data
            const params = await getSignUpBack(`${token_type} ${access_token}`)
            if (!params) return Off()
            if (params.user) setIsPassword(params.user.isDefaultPassword)
            if (params.menu.find(v => v.uuid === '00000000-0000-0000-0000-000000000001')) return setLoading(true)
            else return onAdmin()
        } catch (err) { Off() }
    }

    return (
        <Suspense>
            <div className='SignUpLoading'>
                {loading ?
                    <div className="onSign">
                        {isPassword && <Alert message="默认密码不安全，请及时修改密码！" type="error" />}

                        {/* 选择项目 */}

                        <div>
                            <Button type="primary" size="large" onClick={onAdmin}>后台</Button>
                            <Button size="large" onClick={onStart}>大屏</Button>
                        </div>
                    </div> :
                    <LoadSvg style={{ '--background': 'transparent', '--stroke': '#212121', height: '100%' }} />
                }
            </div>



            <Modal
                title="请选择公司"
                open={open}
                footer={null}
                destroyOnClose
                closable={false}
            >
                {ListByUser.map(item =>
                    <Button type="primary" size='large' style={{ margin: 4 }} block key={item.uuid} onClick={() => signUp(item.uuid)}>{item.tableName}</Button>
                )}
            </Modal>



        </Suspense>

    );
};

export default SignUpLoading